package com.data.dao;

import java.util.HashMap;
import java.util.Map;

import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.base.dao.TemplateDaoImpl;
import com.data.entity.StateShift;
@Repository("stateShiftDao")
public class StateShiftDaoImpl extends TemplateDaoImpl<StateShift, Integer>implements StateShiftDao{
	/**
	 * 根据设备id，班次id，所属日期查询设备状态班次记录
	 * @param equId
	 * @param shiftId
	 * @param belongDate
	 * @return
	 */
	@Override
	public StateShift getStateShiftByIdAndDate(int equId, int shiftId, String stateType,String belongDate) {
		String sql="select * from stateShift where equId=:equId and shiftId=:shiftId and stateType=:stateType and CONVERT(VARCHAR(10),belongDate,120)=:belongDate";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equId", equId);
		params.put("shiftId", shiftId);
		params.put("stateType", stateType);
		params.put("belongDate", belongDate);
		return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(StateShift.class));	
	}

	@Override
	public Double getDurationByShiftAndType(int equId, String stateType,
			String shiftName, String startDate, String endDate) {
		String sql="select  COALESCE(SUM(duration),0) from stateShift where equId=:equId and shiftName=:shiftName and stateType=:stateType and CONVERT(VARCHAR(10),belongDate,120)>=:startDate and CONVERT(VARCHAR(10),belongDate,120)<=:endDate";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equId", equId);
		params.put("shiftName", shiftName);
		params.put("stateType", stateType);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		return jt.queryForObject(sql, params, Double.class);
	
	}

	@Override
	public StateShift getStateShiftBytypeAndDate(int equId, String shiftName,
			String stateType, String belongDate) {
		String sql="select * from stateShift where equId=:equId and shiftName=:shiftName and stateType=:stateType and CONVERT(VARCHAR(10),belongDate,120)=:belongDate";
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("equId", equId);
		params.put("shiftName", shiftName);
		params.put("stateType", stateType);
		params.put("belongDate", belongDate);
		return jt.queryForObject(sql, params, ParameterizedBeanPropertyRowMapper.newInstance(StateShift.class));	
	
	}

}
